﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Data;
using UIT.Controls.Data.Parameters;
using JQueryControlToolkit;
using UIT.Controls;

namespace UIT.Web.Controls.Data
{
    public class WebServiceDataProvider : IClientDataProvider
    {
        private string method;

        public string Method
        {
            get { return method; }
            set
            {
                method = value;
                if (ajax != null)
                    ajax.Type = Method;
            }
        }

        private string serviceUrl;

        public string ServiceUrl
        {
            get { return serviceUrl; }
            set
            {
                serviceUrl = value;
                if (ajax != null)
                    ajax.Url = ServiceUrl + "/" + Operation;
            }
        }

        private string operation;

        public string Operation
        {
            get { return operation; }
            set
            {
                operation = value;
                if (ajax != null)
                    ajax.Url = ServiceUrl + "/" + Operation;
            }
        }


        public ParameterCollection Parameters { get; set; }


        #region IClientDataProvider Members

        JQueryControlToolkit.AjaxQuery ajax;

        public JSObject Render()
        {
            if (ajax == null)
                ajax = JQueryControlToolkit.JQuery.Ajax();
            ajax.Type = Method;
            ajax.Url = ServiceUrl + "/" + Operation;
            ajax.Data = new JQueryControlToolkit.DomResult()
            {
                Operations = 
                { 
                    new JQueryControlToolkit.DomOperation()
                    { 
                        Parameters = this.Parameters.ToDictionary(parameter => parameter.Name, parameter => parameter.Value)
                    } 
                }
            };
            return ajax;
        }

        #endregion

        #region IDataProvider Members

        public string Id
        {
            get;
            set;
        }

        #endregion

        #region IEnumerable Members

        public System.Collections.IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            ajax = null;
            if (Parameters != null)
                Parameters.Dispose();
            Parameters = null;
        }

        #endregion

        #region IDataProvider Members


        public bool CanSelect
        {
            get { return !string.IsNullOrEmpty(serviceUrl) && !string.IsNullOrEmpty(operation); }
        }

        public bool CanInsert
        {
            get { return false; }
        }

        public bool CanDelete
        {
            get { return false; }
        }

        public bool CanUpdate
        {
            get { return false; }
        }

        #endregion


        public System.Collections.IEnumerable Select(SelectArgument arguments)
        {
            throw new NotImplementedException();
        }

        public void Update()
        {
            throw new NotImplementedException();
        }

        public void Insert()
        {
            throw new NotImplementedException();
        }

        public void Delete()
        {
            throw new NotImplementedException();
        }

        public IControl Parent
        {
            get;
            set;
        }

        public Operations SupportedOperations
        {
            get { return CanSelect ? Operations.Select : Operations.None; }
        }
    }
}
